package com.bankcard.bininfo.model;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class IssuerCards {
    private static final Map<String, IssuerCard> ISSUER_CARDS = new ConcurrentHashMap<>();

    public static final IssuerCard BOA_VISA = new IssuerCard(CardBrands.VISA, Issuers.BOA);
    public static final IssuerCard CHASE_MASTER = new IssuerCard(CardBrands.MASTERCARD, Issuers.CHASE);

    static {
        // Hardcoded issuer card data
        register("BOA_VISA", BOA_VISA);
        register("CHASE_MASTER", CHASE_MASTER);
        // Add more issuer cards as needed
    }

    private IssuerCards() {}

    public static void register(String key, IssuerCard issuerCard) {
        ISSUER_CARDS.putIfAbsent(key.toUpperCase(), issuerCard);
    }

    public static IssuerCard getByKey(String key) {
        return ISSUER_CARDS.get(key.toUpperCase());
    }

    public static IssuerCard valueOf(String key) {
        return getByKey(key);
    }

    /**
     * Loads issuer cards from a configuration string.
     * The configuration string should contain entries separated by commas,
     * where each entry is in the format "ISSUER_CARD_BRAND".
     *
     * Example configuration string: "BOA_VISA,CHASE_MASTER"
     *
     * @param config the configuration string containing issuer card data
     */
    public static void loadIssuerCards(String config) {
        try {
            String[] entries = config.split(",");
            for (String entry : entries) {
                String[] parts = entry.split("_");
                if (parts.length == 2) {
                    String issuer = parts[0];
                    String cardBrand = parts[1];
                    IssuerCard issuerCard = new IssuerCard(CardBrands.valueOf(cardBrand), Issuers.valueOf(issuer));
                    register(entry, issuerCard);
                }
            }
        } catch (Exception e) {
            // Log error or handle exception
        }
    }

    public static class IssuerCard {
        private final CardBrand cardBrand;
        private final IssuerInfo issuerInfo;

        public IssuerCard(CardBrand cardBrand, IssuerInfo issuerInfo) {
            this.cardBrand = cardBrand;
            this.issuerInfo = issuerInfo;
        }

        public CardBrand getCardBrand() {
            return cardBrand;
        }

        public IssuerInfo getIssuerInfo() {
            return issuerInfo;
        }
    }
}